package cn.wolfcode.p2p.base.util;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.VerifyCodeVo;
import cn.wolfcode.p2p.base.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class UserContext {

    public static final String USER_IN_SESSION = "loginInfo";
    public static final String VERIFYCODE_IN_SESSION = "verifyCode";
    public static HttpServletRequest getRequest(){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 添加已注册的账户
     * @param count
     */
    public static void setCount(LoginInfo count){
        getRequest().getSession().setAttribute(USER_IN_SESSION,count);
    }
    /**
     * 获取用户对象
     */
    public static LoginInfo getCount(){
        return (LoginInfo) getRequest().getSession().getAttribute(USER_IN_SESSION);
    }

    public static void setVerifyCode(VerifyCodeVo verifyCode){
        getRequest().getSession().setAttribute(VERIFYCODE_IN_SESSION,verifyCode);
    }

    public static VerifyCodeVo getVerifyCode(){
        return (VerifyCodeVo) getRequest().getSession().getAttribute(VERIFYCODE_IN_SESSION);
    }

    //获取ip
    public static String getIP(){
        return getRequest().getRemoteAddr();
    }


}
